package com.cade.agriculturemall.view.personalCenter.collect;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.cade.agriculturemall.R;
import com.cade.agriculturemall.model.GoodsDetailEntity;
import com.cade.agriculturemall.model.ItemFavouriteEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lobby on 2017/12/4.
 */

public class CollectAdapter extends BaseAdapter {
    private List<ItemFavouriteEntity> list = new ArrayList();
    private Context context;

    public CollectAdapter(Context context) {
        this.context = context;
    }


    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_collect, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.titleTv = (TextView) convertView.findViewById(R.id.item_collect_title);
            viewHolder.priceTv = (TextView) convertView.findViewById(R.id.item_collect_price);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.item_collect_imageView);
            convertView.setTag(viewHolder);

//            textView = (TextView) convertView.findViewById(android.R.id.text1);
//            convertView.setTag(textView);
        } else {
//            textView = (TextView) convertView.getTag();
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (list != null) {
            GoodsDetailEntity entity = list.get(position).getProId();
            viewHolder.titleTv.setText(entity.getTitle() + position);
            viewHolder.priceTv.setText("￥ " + entity.getPriceNew());
            if (entity.getPicture().size() == 0) {
            } else {
                Glide.with(context)
                        .load(entity.getPicture().get(0))
//                        .asBitmap().placeholder(R.mipmap.defaule_circle_grey_ava_36dp)
                        .into(viewHolder.imageView);

            }
        }
        return convertView;
    }

    public List<ItemFavouriteEntity> getList() {
        return list;
    }

    public void setList(List<ItemFavouriteEntity> list) {
        this.list = list;
    }

    /**
     * adapter 界面复用
     */
    public static class ViewHolder {
        TextView titleTv;
        TextView priceTv;
        ImageView imageView;
    }
}
